﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZLFMSTInputDialog
{
    public partial class InputBox_PLCInfo : Form
    {
        public InputBox_PLCInfo(int fPLCAddress, int fRegAddrStar, int fRegCount)
        {
            InitializeComponent();
            if ((fPLCAddress > 0) && (fPLCAddress < 128))
                numericUpDownPLCAddress.Value = fPLCAddress;
            else
                numericUpDownPLCAddress.Value = 1;

            if ((fRegAddrStar >= 0) && (fRegAddrStar <= 0xFFFF))
                numericUpDownRegAddrStar.Value = fRegAddrStar;
            else
                numericUpDownRegAddrStar.Value = 0;

            if ((fRegCount > 0) && (fRegCount <= 0xFFFF))
                numericUpDownRegCount.Value = fRegCount;           
            else
                numericUpDownRegCount.Value = 0x12;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        public static bool InputPLCInfoBox(ref int fPLCAddress, ref int fRegAddrStar, ref int fRegCount)
        {
            InputBox_PLCInfo fInputForm = new InputBox_PLCInfo(fPLCAddress, fRegAddrStar, fRegCount);
            if (fInputForm.ShowDialog()== DialogResult.OK)
            {
                fPLCAddress = (int)fInputForm.numericUpDownPLCAddress.Value;
                fRegAddrStar = (int)fInputForm.numericUpDownRegAddrStar.Value;
                fRegCount = (int)fInputForm.numericUpDownRegCount.Value;
                return true;
            }
            return false;
        }

        public static bool InputPLCInfoBox(ref int fPLCAddress, ref int fRegCount)
        {
            int fRegAddrStar = 0;
            return InputPLCInfoBox(ref fPLCAddress,ref fRegAddrStar, ref fRegCount);
        }

        public static bool InputPLCInfoBox(ref int fPLCAddress)
        {
            int fRegAddrStar = 0;
            int fRegCount = 0x12;
            return InputPLCInfoBox(ref fPLCAddress, ref fRegAddrStar, ref fRegCount);
        }        
    }
}
